//
//  SHYSchemaManager.m
//  Pods
//
//  Created by sunhaiyan on 16/6/17.
//
//

#import "SHYSchemaManager.h"
#import "SHYSchemaRouterNode.h"
#import "SHYSchemaRouterNodeHeader.h"
#import "SHYSchemaParseHelper.h"
#import "UIViewController+SchemaNode.h"
#import "NSDictionary+URL.h"
#import "SHYValidJudge.h"

@interface SHYSchemaManager ()

/**
 *  存储node数据的字典 (key是URLString，Value是SHYSchemaRouterNode)
 */
@property (nonatomic, strong) NSMutableDictionary *schemaNodeDict;

@end

@implementation SHYSchemaManager

+ (instancetype)defaultManager {
    static dispatch_once_t onceToken;
    static SHYSchemaManager *rooterSchemaManager = nil;
    dispatch_once(&onceToken, ^{
        rooterSchemaManager = [[SHYSchemaManager alloc] init];
    });
    return rooterSchemaManager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _schemaNodeDict = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (BOOL)registerWithTabName:(NSString *)tabName moduleName:(NSString *)moduleName class:(Class)classz {
    
    SHYSchemaRouterNode * node = [[SHYSchemaRouterNode alloc]initWithSchemeTabName:tabName moduleName:moduleName paramDict:nil controllerClassName:NSStringFromClass(classz)];
    
    if (!node) {
        NSAssert(NO, @"register 失败，node数据错误");
        return NO;
    }
    [self addRouterNode:node];
    return YES;
}

- (NSURL *)schemaUrlForTabName:(NSString *)tabName moduleName:(NSString *)moduleName paramDict:(NSDictionary *)paramDict {
    
    SHYSchemaRouterNode * node = [[SHYSchemaRouterNode alloc]initWithSchemeTabName:tabName moduleName:moduleName paramDict:paramDict controllerClassName:nil];

    if (!node) {
        NSAssert(NO, @"获取schema Url失败，node数据错误");
        return nil;
    }
    return [SHYSchemaParseHelper schemaUrlForSchemaRooterNode:node];
}

#pragma mark - 私有方法

- (void)addRouterNode:(SHYSchemaRouterNode *)node {
    NSString * urlString = [SHYSchemaParseHelper schemaUrlStringHeaderForSchemaRooterNode:node];
    
    if (urlString) {
        if ([self.schemaNodeDict objectForKey:urlString]) {
            // 如果该URL已经有值了表示出错了
            SHYSchemaRouterNode *cacheNode = self.schemaNodeDict[urlString];
            
            NSString * assertString = [NSString stringWithFormat:@"存在2个类的moduleName重复了：old:%@ new:%@",cacheNode.controllerClassName,node.controllerClassName];
            NSAssert(NO, assertString);
        }
        else {
            // 添加成功
            [self.schemaNodeDict setObject:node forKey:urlString];
            NSLog(@"绑定成功：url:%@,node:%@",urlString,node);
        }
    }
}

- (SHYSchemaRouterNode *)nodeForUrl:(NSURL *)url {
    NSArray *urlStrArr = [SHYSchemaParseHelper getSchemaUrlArrayFromParseUrl:url];
    if (urlStrArr.count == 2) {
        // 去掉附加信息的部分就是缓存的key，直接获取node数据；
#warning shy todo 这里要判断tab的位置是否是all，如果不是all，则还需要获取all的
        SHYSchemaRouterNode *node = self.schemaNodeDict[urlStrArr.firstObject];
        node.paramDict = [NSDictionary dictionaryWithURLQuery:urlStrArr.lastObject];
        return node;
    }
    return nil;
}

- (UIViewController *)controllerForNode:(SHYSchemaRouterNode *)node {
    if (node && [SHYValidJudge isValidString:node.controllerClassName]) {
        UIViewController *controller = [NSClassFromString(node.controllerClassName) new];
        controller.additionalDataDict = node.paramDict;
        return controller;
    }
    return nil;
}

- (UIViewController *)controllerForUrl:(NSURL *)url {
    SHYSchemaRouterNode *node = [self nodeForUrl:url];
    return [self controllerForNode:node];
}

- (BOOL)canOpenUrl:(NSURL *)url {
    SHYSchemaRouterNode *node = [self nodeForUrl:url];
    if (node && [SHYValidJudge isValidString:node.controllerClassName]) {
        return YES;
    }
    return NO;
}

- (NSInteger)indexForSelectTabViewController:(NSURL *)url {
    SHYSchemaRouterNode *node = [self nodeForUrl:url];
    if (node.schemaTabName == SHYSchemaRouterNodeHeaderTab1) {
        return 0;
    }
    else if (node.schemaTabName == SHYSchemaRouterNodeHeaderTab2) {
        return 1;
    }
    return 0;
}

@end
